﻿/////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2009 Eric Bowen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
// associated documentation files (the "Software"), to deal in the Software without restriction, including 
// without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
// copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
// following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial 
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/////////////////////////////////////////////////////////////////////////////////////////////////////
// Author's blog: http://blog.scrappydog.com
// Updated source code available:  http://scrappydb.codeplex.com
/////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using NLog;
using ScrappyDB.BaseClasses;

namespace ScrappyDB.Membership
{
    public class MembershipCommon
    {
        private static readonly Logger Log = LogManager.GetCurrentClassLogger();

        private readonly Db _db;

        public MembershipCommon(Db db)
        {
            _db = db;
        }

        public User GetUserByUsernameOrEmail(string usernameOrEmail)
        {
            User user;

            if (usernameOrEmail.Contains("@"))
            {
                //try it as an email address
                string userNameFromEmail = GetUserName(usernameOrEmail);
                user = GetDbUser(userNameFromEmail);

                if (user == null)
                    user = GetDbUser(usernameOrEmail);
            }
            else
            {
                user = GetDbUser(usernameOrEmail);
            }

            return user;
        }

        public Guid GetUserId(string userName)
        {
            User user = GetUserByUsernameOrEmail(userName);
            if (user != null)
                return user.UserId;

            return Guid.Empty;
        }

        public string GetUserName(string email)
        {
            SdbCollection<User> users = _db.Query<User>(" Email = '" + email + "' ");

            if (users == null || users.Count == 0)
                throw new SdbMembershipProviderException("User not found for email: '" + email + "'");

            if (users.Count > 1)
                throw new SdbMembershipProviderException("Multiple Users found for email: '" + email + "'");

            return users[0].UserName;
        }

        private User GetDbUser(string username)
        {
            SdbCollection<User> users = _db.Query<User>(" UserName = '" + username + "' ", true);

            //TODO: log the exception instead if appropriate
            if (users.Count != 1)
            {
                Log.Warn("WARNING: User: '" + username + "' not valid");
                return null;
            }

            User user = users[0];

            return user;
        }
    }
}